<?php
namespace app\controller;

use app\controller\BaseController;
use app\controller\FormValidator\CampFormValidator;
use model\Camp;
use Symfony\Component\HttpFoundation\Request;
use model\PaysQuery;
use model\CampQuery;

class CampController extends BaseController
{
 
    public function newAction(Request $request)
    {
        $session = $this->getSession();
        $flashBag = $this->getFlashBag();
         
        if($request->isMethod('POST'))
        {
            $form = $request->request->all();
            $validator = new CampFormValidator($form);
            $errors = $validator->isValid();
            if(is_bool($errors))
            {
                $pays = PaysQuery::create()->findOneById($form['pays_id']);
                
                $camp = new Camp();
                $camp->setVille($form['ville']);
                $camp->setGpsLat($form['gps_lat']);
                $camp->setGpsLong($form['gps_long']);
                $camp->setPays($pays);
                $camp->setIsPrevention(isset($form['is_prevention']));
                $camp->setIsRefugie(isset($form['is_refugie']));
                $camp->setIsVaccination(isset($form['is_vaccination']));
                $camp->save();
                
                $flashBag->add('success', 'Camp crée avec succès');
                
                return $this->redirect($this->generateUrl('admin_list_camp',array())); // Si il y a des paramêtres dans la route les mettre dans le tableau
                
            }
            else
            {
                $flashBag->add('warning', "Les champs suivant ne sont pas valides ".  implode(', ', $errors));
            }
        }
        
        $pays = PaysQuery::create()->find();
        
        return $this->render('admin/camp/new.php', 
                array('pays'=>$pays));
    }
    
    public function listAction(Request $request)
    {
        $camps = CampQuery::create()->find();
        
        return $this->render('admin/camp/list.php', 
                array('camps' => $camps,
                ));
    }
}